//
//  ViewController.m
//  17-Homework
//
//  Created by dllo on 16/8/6.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "Contact.h"
#import "ContactTableViewCell.h"
#import "ContactDetailViewController.h"
#import "AddContactViewController.h"
#define kTableViewReuse @"reuse"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, AddContactViewControllerDelegate>
@property (nonatomic, strong) NSMutableArray<Contact *> *contactArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSString *path;
@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction:)];
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    self.path = [[array firstObject] stringByAppendingPathComponent:@"contact.plist"];
    [self initContact];
    [self creatTableView];
}

- (void)addAction:(UIBarButtonItem *)barButtonItem {
    AddContactViewController *addVC = [[AddContactViewController alloc] init];
    addVC.delegate = self;
    [self.navigationController pushViewController:addVC animated:YES];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}

- (void)initContact {
    self.contactArray = [NSMutableArray array];
    Contact *contact = [NSKeyedUnarchiver unarchiveObjectWithFile:_path];
    if (contact) {
        [_contactArray addObject:contact];
    }
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.rowHeight = 64;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"ContactTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kTableViewReuse];
    
    
}
#pragma mark - AddContactViewControllerDelegate
- (void)addContact:(Contact *)contact {
    [_contactArray addObject:contact];
    [NSKeyedArchiver archiveRootObject:contact toFile:_path];
    [_tableView reloadData];
}
#pragma mark - TableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _contactArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReuse];
    Contact *contact = _contactArray[indexPath.row];
    cell.contact = contact;
    return cell;
}
#pragma mark - TableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ContactDetailViewController *contactDetailVC = [[ContactDetailViewController alloc] init];
    contactDetailVC.contact = _contactArray[indexPath.row];
    [self.navigationController pushViewController:contactDetailVC animated:YES];
}

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *rowAciton = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [_contactArray removeObjectAtIndex:indexPath.row];
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }];
    return @[rowAciton];
}

#pragma mark - MemoryWarning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
